function searchAnswer(questionId) {
    $.ajax({
        url: '/api/teacher/' + questionId,
        dateType: 'json'
    })
        .done(function (answer) {
            //alert(answer);
            if (answer == null){
                answerDialog(questionId);
                UIkit.modal('#dialog_answer').show();
            }else {
                fillUpAnswerDialog(answer);
                UIkit.modal('#dialog_answer').show();
            }
        })
}
function answerDialog(questionId) {
    var header = $('#dialog_answer h1')[0];
    var paragraph = $('#dialog_answer p');
    header.innerText =questionId ;
    $('#dialog_answer p textarea').innerText="您当前暂未做出回答，请及时解答学生的问题";
    //paragraph[1].innerText = "您当前暂未做出回答，请及时解答学生的问题";
}
function fillUpAnswerDialog(answer) {
    var header = $('#dialog_answer h1')[0];
    var paragraph = $('#dialog_answer p');
    header.innerText = answer.questionId;
    paragraph[0].innerText = answer.answerTime;
    //paragraph[1].innerText = answer.content;
    $('#answer')[0].value = answer.content;
}
var blockingModal;
function updateAnswer() {
    if ($('#answer')[0].value == "尚未有有效回答，请静待做出回答"){
        UIkit.modal.alert("这不是一个有效的回答.");
    }else {
        var answerdata={question_id:parseInt($('#dialog_answer h1')[0].innerText),answer:$('#answer')[0].value};
        //alert(JSON.stringify(answerdata));
        blockingModal = UIkit.modal.blockUI("正在提交...");
        $.ajax({
            url: '/api/teacher/updateAnswer',
            data: JSON.stringify(answerdata),
            method: 'POST',
            contentType: 'application/json'
        })
            .done(function () {
                blockingModal.hide();
                UIkit.modal.blockUI("提交成功！1 秒后将刷新页面。");
                setTimeout(function (args) { window.location.reload(true) }, 1000);
            })
            .fail(function (jqXHR) {
                try {
                    var response = JSON.parse(jqXHR.responseText);
                    if (response != null && response.error != null) {
                        UIkit.modal.alert("Error: " + response.error.message);
                    } else {
                        UIkit.modal.alert("Error occurred.")
                    }
                } catch (e) {
                    UIkit.modal.alert("Error occurred.");
                    console.error(e);
                } finally {
                    blockingModal.hide();
                }
            })
    }

}